package main

import "fmt"
import "sync"
import "time"

var wg sync.WaitGroup
var mutex sync.RWMutex

func write() {
	defer wg.Done()
	mutex.Lock()
	fmt.Println("执行写操作")
	time.Sleep(time.Millisecond * 1000)
	mutex.Unlock()
}

func read() {
	defer wg.Done()
	mutex.RLock()
	fmt.Println("执行读操作")
	time.Sleep(time.Millisecond * 1000)
	mutex.RUnlock()
}

func main() {
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go write()
	}

	for i := 0; i < 10; i++ {
		wg.Add(1)
		go read()
	}
	wg.Wait()
	fmt.Println("main函数结束")
}